<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="http://www.springframework.org/tags" %>
<html>
<head>
    <title>diyishou </title>
    <jsp:include page="../../head.jsp"/>
    <script type="text/javascript">
        //
        var formSetting = {
            title: "查询信息",
            module: 'query',
            columns: [
                {
                    name: "val",
                    label: "供应商名称"
                },
                {
                    name: "status",
                    label: "状态",
                    type: "dropdown",
                    data: [["停用", "0"], ["启用", "1"]]
                }],
            buttons: {
                custom: [{
                             id: "updatebtn0",
                             value: "新增",
                             click: function () {
                                 initCompanyId(null);
                                 initType(null);
                                 $('#f_id').val();

                                 layer.open({
                                                type: 1,
                                                area: ['600px', '250px'],
                                                title: '新增二级供应商',
                                                maxmin: true, //开启最大化最小化按钮
                                                content: $("#winContent"),
                                                btn: ['确定', '关闭'],
                                                yes: function (index, layero) {
                                                    var param = {};
                                                    var url = "/supSrvProvide/save";
                                                    param.supId = $('#f_supId').val();
                                                    param.type = $('#f_type').val();
                                                    param.status = $('#f_status').val();
                                                    param.val =
                                                    $("#f_supId").find("option:selected").text();

                                                    $.ajax({
                                                               type: "POST",
                                                               url: url,
                                                               contentType: "application/json",
                                                               data: JSON.stringify(param),
                                                               success: function (r) {
                                                                   if (r.error_code == 0) {
                                                                       $("#supSrvProvideGrid").trigger("reloadGrid");
                                                                   } else {
                                                                       alert(r.msg);
                                                                   }
                                                                   layer.closeAll();
                                                               }
                                                           });
                                                },
                                                cancel: function (index) {

                                                }
                                            });
                             }
                         },
                         {
                             id: "updatebtn1",
                             value: "修改",
                             click: function () {
                                 var id = $("#supSrvProvideGrid").jqGrid("getGridParam",
                                                                         "selrow");
                                 var id = getSelectedRowsById('supSrvProvideGrid');
                                 if (id == null) {
                                     return;
                                 }

                                 var url = "/supSrvProvide/info/" + id;
                                 $.ajax({
                                            type: "get",
                                            url: url,
                                            dataType: 'json',
                                            success: function (r) {
                                                var supSrvProvide = r.supSrvProvide;
                                                initCompanyId(supSrvProvide.supId);
                                                initType(supSrvProvide.type);

                                                $('#f_id').val(supSrvProvide.id);
                                                $('#f_status').val(supSrvProvide.status);
                                            }
                                        });

                                 layer.open({
                                                type: 1,
                                                area: ['600px', '400px'],
                                                title: '修改二级供应商',
                                                maxmin: true, //开启最大化最小化按钮
                                                content: $("#winContent"),
                                                btn: ['确定', '关闭'],
                                                yes: function (index, layero) {
                                                    var param = {};
                                                    var url = "/supSrvProvide/update";
                                                    param.supId = $('#f_supId').val();
                                                    param.type = $('#f_type').val();
                                                    param.status = $('#f_status').val();
                                                    param.val =
                                                    $("#f_supId").find("option:selected").text();
                                                    param.id = $('#f_id').val();
                                                    $.ajax({
                                                               type: "POST",
                                                               url: url,
                                                               contentType: "application/json",
                                                               data: JSON.stringify(param),
                                                               success: function (r) {
                                                                   if (r.error_code == 0) {
                                                                       $("#supSrvProvideGrid").trigger("reloadGrid");
                                                                   } else {
                                                                       alert(r.msg);
                                                                   }
                                                                   layer.closeAll();
                                                               }
                                                           });
                                                },
                                                cancel: function (index) {

                                                }
                                            });
                             }
                         },
                         {
                             id: "updatebtn2",
                             value: "删除",
                             click: function () {
                                 var ids = getSels("supSrvProvideGrid");
                                 if (ids == null) {
                                     return;
                                 }

                                 $.ajax({
                                            type: "POST",
                                            url: "/supSrvProvide/delete",
                                            contentType: "application/json",
                                            data: JSON.stringify(ids),
                                            success: function (r) {
                                                if (r.error_code == 0) {
                                                    $("#supSrvProvideGrid").trigger("reloadGrid");
                                                } else {
                                                    alert(r.msg);
                                                }
                                            }
                                        });
                             }
                         }]
            },
            grid: {
                id: "supSrvProvideGrid",
                title: "查询结果",
                url: "/supSrvProvide/pageList",
                colModel: [{
                               index: "id",
                               name: "id",
                               key: true
                           }, {
                               name: "val",
                               label: "供应商名称"
                           },
                           {
                               label: '状态', name: 'status', index: 'status',
                               formatter: function (value, options, row) {
                                   if (value == "0") {//0 草稿， 1 生效， -1 无效
                                       return "停用";
                                   } else if (value == "1") {
                                       return " 启用";
                                   }
                               }
                           },
                           {label: '修改时间', name: 'modifyTime', index: 'modify_time'},
                           {label: '修改人', name: 'modifier', index: 'modifier'},
                           {label: '创建时间', name: 'createTime', index: 'create_time'},
                           {label: '创建人', name: 'creator', index: 'creator'}],
                multiselect: true
            }
        };
        $(document).ready(function () {
            $("#table").mrdForm(formSetting);
            doResize();
            $(window).resize(function () {
                doResize();
            });

            $(".ui-jqgrid-title").text("二级供应商列表");
        });

        function doResize() {
            var ss = getPageSize();
            $("#originalDeclarationGrid").jqGrid('setGridHeight', ss.WinH - 240);
        }

        function getPageSize() {
            var winW, winH;
            if (window.innerHeight) {// all except IE
                winW = window.innerWidth;
                winH = window.innerHeight;
            } else if (document.documentElement && document.documentElement.clientHeight) {// IE 6 Strict Mode
                winW = document.documentElement.clientWidth;
                winH = document.documentElement.clientHeight;
            } else if (document.body) { // other
                winW = document.body.clientWidth;
                winH = document.body.clientHeight;
            }
            return {WinW: winW, WinH: winH};
        }

        //选择多条记录
        function getSels(gridId) {
            var grid = $("#" + gridId);
            var rowKey = grid.getGridParam("selrow");
            if (!rowKey) {
                alert("请选择一条记录");
                return;
            }

            return grid.getGridParam("selarrrow");
        }

        //选择多条记录
        function getSelectedRowsById(gridId) {
            var grid = $("#" + gridId);
            var rowKey = grid.getGridParam("selrow");
            if (!rowKey) {
                alert("请选择一条记录");
                return;
            }

            return grid.getGridParam("selarrrow");
        }

        /**
         * 初始化供应商下拉框
         * @author han
         **/
        function initCompanyId(supId) {
            $.ajax({
                       type: "get",
                       url: "/supSrvProvide/getCompanyList",
                       dataType: 'json',
                       success: function (data) {
                           if (data != null) {
                               var companySelect = $('#f_supId');
                               companySelect.empty();
                               for (var i = 0; i < data.length; i++) {
                                   var selected = "";
                                   if (supId != null && supId == data[i].id) {
                                       selected = "selected='selected'";
                                   }
                                   companySelect.append("<option value='" + data[i].id + "' "
                                                        + selected + ">" + data[i].name
                                                        + "</option>");
                               }
                           }
                       }
                   });
        }

        /**
         * 初始化类别
         * @author han
         **/
        function initType(typeVal) {
            $.ajax({
                       type: "get",
                       url: "/api/dictionary/search?dtGroupCode=SUP_SERVICE_PROVIDE",
                       dataType: 'json',
                       success: function (data) {
                           if (data.content != null) {
                               var typeSelect = $('#f_type');
                               typeSelect.empty();
                               var dicList = data.content;
                               for (var i = 0; i < dicList.length; i++) {
                                   var selected = "";
                                   if (typeVal != null && typeVal == dicList[i].dtKey) {
                                       selected = "selected='selected'";
                                   }
                                   typeSelect.append("<option value='" + dicList[i].dtKey + "' "
                                                     + selected + ">" + dicList[i].dtValue
                                                     + "</option>");
                               }
                           }
                       }
                   });
        }
    </script>
</head>
<body>
<div id="table"></div>
<div id="winContent" style="display:none;margin:5px 5px 5px 5px;">
    <table width="100%" border="1" cellpadding="1" bordercolor="#eeeeee">
        <tr bgcolor="#FFFFFF">
            <td width="198" height="38" align="right" bgcolor="#f8f8f8">供应商：</td>
            <td width="345" align="left">
                <input type="hidden" id="f_id" name="f_id"/>
                <select id="f_supId" name="f_supId">
                </select>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td height="38" align="right" bgcolor="#f8f8f8">类别：</td>
            <td align="left">
                <select id="f_type" name="f_type">
                </select>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td align="right" height="38" bgcolor="#f8f8f8">状态：</td>
            <td align="left">
                <select id="f_status" name="f_status">
                    <option value="0">停用</option>
                    <option value="1">启用</option>
                </select>
            </td>
        </tr>
    </table>
</div>
</body>
</html>
